package com.pagepub.expo.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.pagepub.expo.entity.AppRole;
import com.pagepub.expo.entity.RoleFunction;
import com.pagepub.expo.manager.AppRoleMng;
import com.pagepub.expo.manager.AppSeqMng;
import com.pagepub.expo.manager.RoleFunctionMng;

public class AppRoleAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("appRoleMng")
	private AppRoleMng appRoleMng;

	@Autowired
	@Qualifier("appSeqMng")
	private AppSeqMng appSeqMng;
	
	@Autowired
	@Qualifier("roleFunctionMng")
	private RoleFunctionMng roleFunctionMng;
	

	public List<AppRole> rows = new ArrayList<AppRole>();

	public int start = 0;

	public int limit = 0;
	public int total;
	public long newId = 0;
	private boolean success;
	private String roleListStr;
	
	public String getRoleListStr() {
		return roleListStr;
	}

	public void setRoleListStr(String roleListStr) {
		this.roleListStr = roleListStr;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	private AppRole appRole;

	public AppRoleAction() {
		System.out.println("this is AppDictAction");
	}

	public String execute() throws Exception {
		System.out.println("this is execute");
		return null;
	}

	public String all() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int start = Integer.valueOf(request.getParameter("start"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		String sortname = request.getParameter("sortname");
		String sortorder = request.getParameter("sortorder");
		HashMap<String, String> param = new HashMap<String, String>();
		if (sortname != null && sortname.length() > 0) {
			param.put("sortname", sortname);
		}
		if (sortorder != null && sortorder.length() > 0) {
			param.put("sortorder", sortorder);
		}
		rows = appRoleMng.findByPage(start, limit, param);
		this.total = appRoleMng.getCount(param);
		success = true;
		return SUCCESS;
	}

	public String getRoleListForCheckbox() {
		List<AppRole> temp = appRoleMng.findAll();
		StringBuffer str = new StringBuffer();
		str.append('[');
		for (int i = 0; i < temp.size(); i++) {
			str.append("{boxLabel:'");
			str.append(temp.get(i).getRoleName());
			str.append("' , name:'selectedRoles', inputValue:");
			str.append(temp.get(i).getRoleId());
			str.append(", id:'role"+temp.get(i).getRoleId()+"'");
			str.append('}');
			if(i<temp.size()-1)
				str.append(',');
		}
		str.append(']');
		roleListStr = str.toString();
		success = true;
		return SUCCESS;
	}

	public String delete() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("delIds");
		ids = ids.substring(0, ids.length() - 1);
		StringTokenizer st = new StringTokenizer(ids, ",", false);
		List<String> StringList = new ArrayList<String>();
		while (st.hasMoreElements()) {
			StringList.add(st.nextToken());
		}
		for (int i = 0; i < StringList.size(); i++) {
			appRoleMng.remove(new Long(StringList.get(i)));
		}
		success = true;
		return SUCCESS;
	}

	public String add() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String seqName = request.getParameter("seqName");
		newId = appSeqMng.getSequence(seqName);
		AppRole appRole = new AppRole();
		appRole.setRoleId(new Long(newId));
		appRole.setRoleName("");
		appRole.setRoleDesc("");
		appRole.setRights("");
		appRole.setStatus((short) 1);
		appRoleMng.add(appRole);
		success = true;
		return SUCCESS;
	}

	public String modify() {
		appRoleMng.update(appRole);
		success = true;
		return SUCCESS;
	}

	public long getNewId() {
		return newId;
	}

	public void setNewId(long newId) {
		this.newId = newId;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public AppRole getAppRole() {
		return appRole;
	}

	public void setAppRole(AppRole appRole) {
		this.appRole = appRole;
	}

	public List<AppRole> getRows() {
		return rows;
	}

	public void setRows(List<AppRole> rows) {
		this.rows = rows;
	}
	
	
	private String roleOwnFunIdList ;
	public String roleOwnFuns() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Long roleId = Long.valueOf(request.getParameter("roleId"));
		List<RoleFunction> rflist = roleFunctionMng.findAllByRole(roleId);
		StringBuffer sb = new StringBuffer();
		for(int i = 0 ;i < rflist.size(); i++) {
			sb.append(rflist.get(i).getFunctionId());
			if(i<rflist.size()-1)
				sb.append(',');
		}
		roleOwnFunIdList = sb.toString();
		success = true;
		return SUCCESS;
	}

	public String getRoleOwnFunIdList() {
		return roleOwnFunIdList;
	}

	public void setRoleOwnFunIdList(String roleOwnFunIdList) {
		this.roleOwnFunIdList = roleOwnFunIdList;
	}

	
	public String assignFun() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = (String)request.getParameter("ids");
		Long roleId = Long.valueOf(request.getParameter("roleId"));
		
		StringTokenizer st = new StringTokenizer(ids, ",", false);
		List<String> StringList = new ArrayList<String>();
		while (st.hasMoreElements()) {
			StringList.add(st.nextToken());
		}
		
		roleFunctionMng.deleteByRole(roleId);
		for (int i = 0; i < StringList.size(); i++) {
			RoleFunction rf = new RoleFunction();
			rf.setRoleId(roleId);
			rf.setFunctionId(new Long(StringList.get(i).trim()));
			roleFunctionMng.insert(rf);
		}
		success = true;
		return SUCCESS;
	}
	
}
